package sma.fbss.model;

import sma.fbss.database.Database;
//import ifi.sma.mss.utils.Accessary;

import java.util.ArrayList;
//import java.util.Date;
import java.util.Map;

public class MDBuyer {
	public String id_club = "";
	public String name_club;
	public String description;
	public String AID;

	public MDBuyer() {
	}

	public MDBuyer(String name) {
		if (name.equals(""))
			return;
		try {
			AID = name;
			name = name.substring(0, name.indexOf("@"));
			Database database = new Database("FBMarket");
			String str_sql = "select * from club where id_club = '" + name + "'";
			ArrayList<Map<String, String>> datas = database.get(str_sql);
			if (datas.size() > 0) {
				this.id_club = datas.get(0).get("id_club").toString();
				this.name_club = datas.get(0).get("name_club");
				this.description = datas.get(0).get("description");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setBuy(MDSeller sl, MDFBaller fbl, long date, String descrip) {
		try {
			Database database = new Database("FBMarket");
			String str_sql = "insert into BUYPURCHASE(club_buyer, club_seller, footballeur," +
			"price, date_purchase, description) values('" + this.id_club + "','" + 
			sl.id_club + "','" + fbl.id_footballer + "','" + fbl.price + "','" + 
			date + "','" + descrip + "')";
			database.set(str_sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean update() {
		try {
			String str_sql = "";
			Database database = new Database("FBMarket");
			if (this.id_club == "") {

				str_sql = "INSERT INTO club(id_club, name_club, description) VALUES('" + 
				this.id_club + "','" + this.name_club + "','" + this.description + "')";
				database.set(str_sql);
				return true;
			} else {
				str_sql = "UPDATE club set name_club = '" + this.name_club + 
						"', description = '" + this.description + "' WHERE id_club = "
						+ this.id_club;
				database.set(str_sql);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}

